import { defineStore } from 'pinia'
import { getTableIdApi, postTableApi, postOrderApi } from '@/api/worktable'


export const useWorktableStore = defineStore('worktable', {
    state: () => {
        return {
            orderAll: [],
            serveTable: [],
            freeTable: [],
            totalTable: [],
            orderUnpay: []
        }
    },

    getters: {

    },

    actions: {
        // 通过number查询台桌_id
        async getTableId(number) {
            const res = await getTableIdApi({ number })
            return res
        },
        // 释放台桌
        async modifyTable({ _id, cut, number, position, status }) {
            await postTableApi({ _id, cut, number, position, state: '0', status })
        },
        // 将订单改为已结账
        async modifyOrder({ _id, tableNum, totalMoney, peopleNum, dishInfo, notes }) {
            await postOrderApi({ _id, tableNum, totalMoney, peopleNum, dishInfo, notes, status: 1 })
        },
        async modifyTableNum({ _id, totalMoney, peopleNum, dishInfo, notes, status }, tableInfo, orderInfo) {
            // 修改订单桌号
            await postOrderApi({ _id, tableNum: tableInfo.number, totalMoney, peopleNum, dishInfo, notes, status })
            // 修改新桌子状态为使用
            await postTableApi({ ...tableInfo, state: '1' })
            // 通过旧桌号查询旧桌子_id
            const res = await getTableIdApi({ number: orderInfo.tableNum })
            const oldTableInfo = res.data[0]
            // 修改旧桌子状态为空闲
            await postTableApi({ ...oldTableInfo, state: '0' })
        }
    }
})